package net.dgg.rz.production.service;

import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.iboss.base.util.DateUtil;
import net.dgg.rz.common.base.service.BaseService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.utils.DateUtils;
import net.dgg.rz.production.dao.ProductNodeFlowDao;
import net.dgg.rz.production.dao.ProductTimeStatusDao;
import net.dgg.rz.production.entity.ProductNodeFlow;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.entity.ProductTimeStatus;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zyou on 2019/1/29.
 */
@Service
public class ProductTimeStatusService extends BaseService {
    @Autowired
    private CmsService cmsService;
    @Autowired
    private ProductNodeFlowDao productNodeFlowDao;
    @Autowired
    private ProductTimeStatusDao productTimeStatusDao;
    @Autowired
    private SessionManager sessionManager;

    @Autowired
    private CommonMangerExtService commonMangerExtService;

    @Autowired
    private UserService userService;

    public ProductTimeStatus insertProductStatus(ProductOrder productOrder){
        Date now = new Date();
        ProductTimeStatus timestatus = new ProductTimeStatus();
        timestatus.setId(productOrder.getId());
        timestatus.setScProductOrderNo(productOrder.getScProductOrderNo());
        timestatus.setNodeFlowId(productOrder.getNodeFlowId());
        Map maps = new HashMap<>();
        maps.put("deptIds", productOrder.getProductOrgId());
        maps.put("businessCode", productOrder.getBusinessProductCode());
        ProductNodeFlow node = productNodeFlowDao.queryFlowByMap(maps);
        DggValidateUtil.notNull(node, CommonExcption.class, "未查询到"+productOrder.getProductOrgName()+"["+productOrder.getBusinessProductName()+"]流程节点的配置信息");
        if(node.getIsnaturalorworkday()==1) {//工作日
            Date nowDate = new Date();
            String orderEndTime = cmsService.findAfterDate(DateUtil.dateToString(nowDate,"yyyy-MM-dd HH:mm:ss"),node.getManagementCycle())+" "+DateUtil.dateToString(nowDate,"HH:mm:ss");
            timestatus.setOrderEndTime(DateUtil.stringToDate(orderEndTime,"yyyy-MM-dd HH:mm:ss"));
        }else {
            try {
                timestatus.setOrderEndTime(DateUtils.dateAddDayJs(new Date(), node.getManagementCycle()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        timestatus.setOrderTimeStatus(ScProductConstant.NORMAL);
        timestatus.setDistributionStartTime(now);//计时开始时间
        timestatus.setDistributionTimeOutStatus(ScProductConstant.NORMAL);
        try {
            timestatus.setDistributionEndTime(DateUtils.dateAddDayJs(now, 1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(productOrder.getFlowUserId() != null){
            timestatus.setReceiveStartTime(now);
            timestatus.setReceiveProcessTime(1);
            try {
                timestatus.setReceiveEndTime(DateUtils.dateAddDayJs(now, 1));
                timestatus.setReceiveTimeStatus(ScProductConstant.NORMAL);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        timestatus.setFlag(1);//必须要加入
        timestatus.setNodeTimeStatus(ScProductConstant.NORMAL);//写入节点超期状态,默认不超期
        timestatus.setOrderTimeStatus(ScProductConstant.NORMAL);
        UserEntity userEntity = sessionManager.getCurrentUser();
        this.saveOpt(timestatus,userEntity);
        Map curentDept = null;//当前登录人部门
        if (StringUtils.isEmpty(userEntity.getId())){
            //传过来的创建者
            userEntity = userService.findUserById(userEntity.getId());
            curentDept = this.commonMangerExtService.findOrg(userEntity.getOrgId());
        }

        DggValidateUtil.notNull(userEntity,CommonExcption.class,"登陆人为空，请重新登陆！");
        timestatus.setCreaterOrgName(null == curentDept ? null : curentDept.get("name").toString());
        timestatus.setUpdaterOrgName(null == curentDept ? null : curentDept.get("name").toString());
        timestatus.setOrderStartTime(now);
        timestatus.setOrderProcessTime(node.getManagementCycle());
        timestatus.setOrderProcessTimeType(node.getIsnaturalorworkday());
        productTimeStatusDao.insertSelective(timestatus);
        return timestatus;
    }
}
